package com.search.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

//全局过滤器
@Component
public class AuthorizeFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
        System.out.println(queryParams);
        String authorization = queryParams.getFirst("Auth");
        System.out.println(authorization);
        if (authorization != null && authorization != "") {

            if (authorization.matches("^/d+$")) {
                return chain.filter(exchange);
            }
        }
        //todo 配置里的过滤器没有生效 （没找到原因 下午尝试手动全局过滤器加请求头）
        System.out.println("Autho出错 ！！！！！！！");
        return chain.filter(exchange);
//        exchange.getResponse().setStatusCode(HttpStatus.FORBIDDEN);
//        return exchange.getResponse().setComplete();
    }

    @Override
    public int getOrder() {
        return 2;
    }
}